export default class Request {
	#event;
	#context;
	#config;
	#headers;
	#CONTENT_TYPE = 'content-type';
	#JSON_TYPE = 'application/json';
	#FORM_TYPE = 'application/x-www-form-urlencoded';
	#TEXT_TYPE = 'text/xml';
	#token;
	#token_expired;
	#actions = [];
	#data;
	#httpMethod;
	#action;
	#body;
	#queryStringParameters;
	constructor(event, context, config) {
		this.#event = event;
		this.#context = context;
		this.#config = config;
		let { data, headers = {}, httpMethod, queryStringParameters = {}, isBase64Encoded, body } = event;
		this.#headers = headers;
		this.#data = data;
		this.#httpMethod = httpMethod;
		//如果是base64
		if (isBase64Encoded) {
			body = Buffer.from(body, 'base64').toString();
		}
		this.#body = body;
		this.#queryStringParameters = queryStringParameters;

		if (!event.action && event.path) {
			event.action = event.path[0] == '/' ? event.path.substr(1) : event.path;
		}
		let actionStr = event.action || '';
		if (actionStr.startsWith('/')) {
			event.action = actionStr.substr(1);
		}
		this.#action = event.action;
		this.#actions = actionStr.split('/');
	}
	//获取get请求参数
	request(name = '', value = '') {
		return this.get(name, value);
	}
	//获取get请求参数
	get(name = '', value = '') {
		if (typeof name != 'string') throw new Error('name is must be a string');
		if (name) {
			return this.#queryStringParameters[name] || value;
		} else {
			return {
				...this.#queryStringParameters,
			};
		}
	}
	//获取post请求参数
	post(name = '', value = '') {
		if (typeof name != 'string') throw new Error('name is must be a string');
		let param = {};
		//还需整合
		if (this.#body) {
			const contextType = this.#headers[this.#CONTENT_TYPE];
			if (contextType === this.#FORM_TYPE) {
				param = qs.parse(this.#body);
			} else if (contextType == this.#TEXT_TYPE) {
				//文本直接返回
				return this.#body;
			} else if (contextType == this.#JSON_TYPE) {
				try {
					param = JSON.parse(this.#body);
				} catch (e) {
					//不是json。返回
					return this.#body;
				}
			} else {
				//其他的暂时直接返回
				return this.#body;
			}
		} else {
			param = this.#data;
		}
		if (name) {
			return param[name] || value;
		} else {
			return { ...param };
		}
	}
	// 路径参数
	pathParam(name = '', value = '') {
		if (typeof name != 'string') throw new Error('name is must be a string');
		let paths = [];
		if (this.#config.app_multi_module) {
			paths = this.#actions.slice(3);
		} else {
			paths = this.#actions.slice(2);
		}
		const param = {};
		paths.map((item, index) => {
			if (index % 2 == 0 && !!item) {
				param[item] = paths[index + 1];
			}
		});
		if (name) {
			return param[name] || value;
		} else {
			return param;
		}
	}
	//get和post参数
	param(name = '', value = '') {
		if (typeof name != 'string') throw new Error('name is must be a string');
		let p = this.post();
		p =
			Object.prototype.toString.call(p) === '[object Object]'
				? p
				: {
						post: p,
				  };
		let param = Object.assign({}, this.pathParam(), this.get(), p);
		if (name) {
			return param[name] || value;
		} else {
			return param;
		}
	}
	//获取路径信息
	get url() {
		return this.#action;
	}
	header(name = '') {
		if (typeof name != 'string') throw new Error('name is must be a string');
		if (name) {
			return this.#headers[name];
		} else {
			return { ...this.#headers };
		}
	}
	get lang() {
		const lang = this.#headers.lang || this.#event.lang;
		if (lang && this.#config.app_lang_list && !this.#config.app_lang_list.includes(lang)) {
			throw new Error('语言参数错误');
		}
		return lang || this.#config.app_lang;
	}
	get method() {
		return this.#httpMethod;
	}
	get ip() {
		return this.#context.CLIENTIP || this.#headers['x-real-ip'];
	}
	get ua() {
		return this.#context.CLIENTUA || this.#context.ua;
	}
	get module() {
		if (this.#config.app_multi_module) {
			return this.#actions[0] || this.#config.app_module;
		}
		return '';
	}
	//controller
	get controller() {
		let controller = '';
		if (this.#config.app_multi_module) {
			controller = this.#actions[1];
		} else {
			controller = this.#actions[0];
		}
		return controller || this.#config.app_controller;
	}
	// action
	get action() {
		let actioned = '';
		if (this.#config.app_multi_module) {
			actioned = this.#actions[2];
		} else {
			actioned = this.#actions[1];
		}
		return actioned || this.#config.app_action;
	}
	//当前服务空间信息
	get spaceInfo() {
		return this.#context.SPACEINFO;
	}
	get appId() {
		return this.#context.APPID || this.#context.appId;
	}
	get deviceId() {
		return this.#context.DEVICEID || this.#context.deviceId;
	}
	get functionName() {
		return this.#context.FUNCTION_NAME || '';
	}
	get functionType() {
		return this.#context.FUNCTION_TYPE || '';
	}
	//获取token
	get uniIdToken() {
		return this.#event.uniIdToken;
	}
	//token
	token(value = '') {
		if (value) {
			this.#token = value;
		} else {
			return this.#token;
		}
	}
	//token
	tokenExpired(value = '') {
		if (value) {
			this.#token_expired = value;
		} else {
			return this.#token_expired;
		}
	}
	get isTriggerTimer(){
		return this.#event['Type'] === 'Timer';
	}
	//event
	get event() {
		return this.#event;
	}
	//context
	get context() {
		return this.#context;
	}
}
